const crypto = require('crypto')
const config = require('../config/config')

const encryptWithMD5 = sourceString => {
  const hash = crypto.createHash('md5')
  hash.update(sourceString)
  return hash.digest('hex')
}

const encryptPassword = password => {
  return encryptWithMD5(password)
}

/**
 * 根据密码按一定的规则生成加密token的secret
 * @param {string} password
 */
const getSecret = password => {
  return encryptWithMD5(config.getConf('security:jwt:secret') + password)
}

module.exports = {
  encryptPassword,
  getSecret,
  encryptWithMD5
}
